package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 989. 数组形式的整数加法      https://leetcode.cn/problems/add-to-array-form-of-integer/description
 */
public class LeetCode989 {

    // 输入：num = [2,1,5], k = 806
    // 输出：[1,0,2,1]
    // 解释：215 + 806 = 1021
    public static List<Integer> addToArrayForm(int[] num, int k) {
        int carry = 0;
        List<Integer> res = new ArrayList<>();
        for (int i = num.length - 1; i >= 0; i--) {
            int mod = (num[i] + (k % 10) + carry) % 10;
            res.add(mod);
            carry = (num[i] + (k % 10) + carry) / 10;
            k /= 10;
        }
        while (k > 0) {
            res.add((k % 10 + carry) % 10);
            carry = ((k % 10) + carry) / 10;
            k /= 10;
        }
        if (carry > 0) {
            res.add(carry);
        }
        Collections.reverse(res);
        return res;
    }

    public static void main(String[] args) {
        int[] num = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
        List<Integer> list = addToArrayForm(num, 1);
        System.out.println(list);
    }
}
